<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home/welcome' }"
        >首页</el-breadcrumb-item
      >
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 添加商品分类按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary" class="classifyBtn" @click="classifyBtn"
            >添加分类</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table
        :data="goodsList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
      >
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="!scope.row.cat_deleted"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <template slot="sort" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="warning" v-else-if="scope.row.cat_level === 2"
            >三级</el-tag
          >
        </template>
        <template slot="operation" slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            @click="editclassifyBtn(scope.row)"
            >编辑</el-button
          >
          <el-button type="danger" icon="el-icon-delete" @click="deleteClassify(scope.row)">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="cateSizeChange"
        @current-change="cateCurrentChange"
        :current-page="queryInofo.pagenum"
        :page-sizes="[1, 5, 10, 15]"
        :page-size="queryInofo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="classifyVisible"
      width="50%"
      :before-close="classifyClose"
      @close="resetClassifyForm"
    >
      <!-- 添加分类表单 -->
      <el-form
        :model="addClassifyForm"
        :rules="addClassifyFormrules"
        ref="addClassifyFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addClassifyForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级选择:">
          <div>
            <el-cascader
              v-model="selectedKey"
              :options="parentInfo"
              :props="{
                expandTrigger: 'hover',
                value: 'cat_id',
                label: 'cat_name',
                checkStrictly: true
              }"
              @change="parentChange"
              clearable
              ref="parentCascaderRef"
            ></el-cascader>
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="classifyVisible = false">取 消</el-button>
        <el-button type="primary" @click="addClassifyBtn">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 商品分类编辑对话框 -->
    <el-dialog
      title="编辑商品分类"
      :visible.sync="editclassifyVisible"
      width="50%"
      @close="resetEditClassifyForm"
    >
      <!-- 添加分类表单 -->
      <el-form
        :model="editClassifyForm"
        :rules="editClassifyFormrules"
        ref="editClassifyFormRef"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="editClassifyForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="editclassifyVisible = false">取 消</el-button>
        <el-button type="primary" @click="editClassifyConfirm">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 导入api
import {
  getGoodsApi,
  getAddClassifyApi,
  getEditClassifyApi,
  getDeleteClassifyApi
} from '@/api/goods.js'

export default {
  name: 'Cate',
  data() {
    return {
      // 查询条件
      queryInofo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 商品分类数据
      goodsList: [],
      // 总数据条数
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'sort'
        },
        {
          label: '操作',
          type: 'template',
          template: 'operation'
        }
      ],
      // 是否显示添加分类对话框
      classifyVisible: false,
      // 添加分类表单数据
      addClassifyForm: {
        // 分类名称
        cat_name: '',
        // 分类ID
        cat_pid: 0,
        // 分类层级
        cat_level: 0
      },
      // 添加分类表单验证
      addClassifyFormrules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 父级分类列表数据
      parentInfo: [],
      // 选中的父级分类的ID
      selectedKey: [],
      // 控制编辑商品分类对话框显示与隐藏
      editclassifyVisible: false,
      // 商品分类编辑对话框的表单双向绑定数据对象
      editClassifyForm: {
        cat_name: ''
      },
      // 编辑商品分类表单验证
      editClassifyFormrules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 当前编辑的商品分类的id
      editClassifyId: 0
    }
  },
  created() {
    this.getGoodsApi()
  },
  methods: {
    // 商品分类数据列表请求
    getGoodsApi() {
      getGoodsApi(this.queryInofo).then((res) => {
        if (res.data.meta.status === 200) {
          this.$message.success('获取商品分类数据成功')
          // 赋值
          this.goodsList = res.data.data.result
          this.total = res.data.data.total
        } else {
          this.$message.error('获取商品分类数据失败')
        }
      })
    },
    // 父级分类列表请求
    getGoodsParentApi() {
      getGoodsApi({ type: 2 }).then((res) => {
        if (res.data.meta.status !== 200) {
          return this.$message.error('获取父级分类列表失败')
        }
        this.parentInfo = res.data.data
        console.log(this.parentInfo)
      })
    },
    // 确定添加分类请求
    getAddClassifyApi() {
      getAddClassifyApi(this.addClassifyForm).then((res) => {
        console.log(res)
        if (res.data.meta.status !== 201) {
          return this.$message.error('添加分类失败')
        }
        this.$message.success('添加分类成功')
      })
    },
    // 商品分类编辑请求
    getEditClassifyApi() {
      getEditClassifyApi(this.editClassifyId, this.editClassifyForm).then(
        (res) => {
          if (res.data.meta.status !== 200) {
            return this.$message.error('编辑失败')
          }
          this.$message.success('编辑成功')
          // 刷新页面
          this.getGoodsApi()
        }
      )
    },
    // 监听 pagesize 改变
    cateSizeChange(newSize) {
      this.queryInofo.pagesize = newSize
      this.getGoodsApi()
    },
    // 监听 pagenum 改变
    cateCurrentChange(newPagenum) {
      this.queryInofo.pagenum = newPagenum
      this.getGoodsApi()
    },
    // 点击添加分类按弹出对话框
    classifyBtn() {
      this.classifyVisible = true
      this.getGoodsParentApi()
    },
    // 点击X关闭添加分类对话框的回调
    classifyClose() {
      this.classifyVisible = false
    },
    // 父级分类选择框变化触发
    parentChange() {
      this.$refs.parentCascaderRef.dropDownVisible = false
    },
    // 确定添加分类
    addClassifyBtn() {
      if (this.selectedKey.length > 0) {
        this.addClassifyForm.cat_pid =
          this.selectedKey[this.selectedKey.length - 1]
        this.addClassifyForm.cat_level = this.selectedKey.length
      } else {
        this.addClassifyForm.cat_pid = 0
        this.addClassifyForm.cat_level = 0
      }

      this.$refs.addClassifyFormRef.validate((valid) => {
        if (!valid) return
        this.getAddClassifyApi()
        this.classifyVisible = false
      })
    },
    // 重置添加分类表单
    resetClassifyForm() {
      this.$refs.addClassifyFormRef.resetFields()
      this.selectedKey = []
    },
    // 点击商品分类编辑按钮触发
    editclassifyBtn(row) {
      this.editclassifyVisible = true
      this.editClassifyForm.cat_name = row.cat_name
      this.editClassifyId = row.cat_id
    },
    // 确定提交编辑触发
    editClassifyConfirm() {
      this.$refs.editClassifyFormRef.validate((valid) => {
        if (!valid) return
        this.getEditClassifyApi()
        this.editclassifyVisible = false
      })
    },
    // 关闭编辑对话框时触发
    resetEditClassifyForm() {
      this.$refs.editClassifyFormRef.resetFields()
    },
    // 删除商品分类
    deleteClassify(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 调用请求
          getDeleteClassifyApi(row.cat_id).then(res => {
            if (res.data.meta.status !== 200) {
              return this.$message.error('删除失败')
            }
            this.$message.success('删除成功')
            // 刷新页面
            this.getGoodsApi()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 15px;
}

.classifyBtn {
  margin-bottom: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
